package com.wanghua.mall.product.service.impl;

import com.wanghua.mall.product.entity.AttrEntity;
import com.wanghua.mall.product.entity.ProductAttrValueEntity;
import com.wanghua.mall.product.frontvo.SkuItemVo;
import com.wanghua.mall.product.frontvo.SpuItemAttrGroup;
import com.wanghua.mall.product.service.AttrService;
import com.wanghua.mall.product.service.CategoryService;
import com.wanghua.mall.product.service.ProductAttrValueService;
import com.wanghua.mall.product.vo.AttrAndGroupVo;
import com.wanghua.mall.product.vo.AttrGroupResponseVo;
import com.wanghua.mall.product.vo.AttrResponseVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wanghua.common.utils.PageUtils;
import com.wanghua.common.utils.Query;

import com.wanghua.mall.product.dao.AttrGroupDao;
import com.wanghua.mall.product.entity.AttrGroupEntity;
import com.wanghua.mall.product.service.AttrGroupService;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;


@Service
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
    @Resource
    private CategoryService categoryService;
    @Resource
    private AttrService attrService;
    @Resource
    private ProductAttrValueService productAttrValueService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils listByCategoryId(Map<String, Object> params, Long categoryId) {
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<>();
        if (categoryId != 0) {
            queryWrapper.eq("category_id", categoryId);
        }
        String key = (String) params.get("key");
        if (StringUtils.hasLength(key)) {
            queryWrapper.like("name", key);
        }
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                queryWrapper
        );
        return new PageUtils(page);
    }

    /**
     * 查询属性分组完整信息
     */
    @Override
    public AttrGroupResponseVo infoDetail(Long id) {
        AttrGroupResponseVo attrGroupResponseVo = new AttrGroupResponseVo();
        AttrGroupEntity attrGroupEntity = baseMapper.selectById(id);
        BeanUtils.copyProperties(attrGroupEntity, attrGroupResponseVo);
        //设置属性分组的完整分类路径
        Long[] categoryPath = categoryService.findCategoryPath(attrGroupEntity.getCategoryId());
        attrGroupResponseVo.setCategoryPath(categoryPath);

        return attrGroupResponseVo;
    }

    /**
     * 删除分组时，连带设置所关联的基本属性的分组id字段为0
     */
    @Override
    public void deleteDetail(Long[] ids) {
        //先删除属性分组表
        baseMapper.deleteBatchIds(Arrays.asList(ids));
        //再设置所关联的基本属性的分组id字段为0
        for (Long attrGroupId : ids) {
            QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("attr_group_id", attrGroupId);
            List<AttrEntity> attrEntityList = attrService.list(queryWrapper);
            attrEntityList.forEach((attrEntity) -> {
                attrEntity.setAttrGroupId(0L);
                attrService.updateById(attrEntity);
            });
        }
    }

    //获取分类下所有分组&关联属性
    @Override
    public List<AttrAndGroupVo> getAllAttrGroupAndAttrByCategoryId(Long categoryId) {
        List<AttrAndGroupVo> result = new ArrayList<>();
        //查出分类下的所有分组
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id", categoryId);
        List<AttrGroupEntity> attrGroupEntityList = baseMapper.selectList(queryWrapper);

        //普通方法
        //查出所有分组下的所有属性
//        for (AttrGroupEntity attrGroupEntity : attrGroupEntityList) {
//            AttrAndGroupVo attrAndGroupVo = new AttrAndGroupVo();
//            BeanUtils.copyProperties(attrGroupEntity, attrAndGroupVo);
//            QueryWrapper<AttrEntity> wrapper = new QueryWrapper<>();
//            wrapper.eq("attr_group_id", attrGroupEntity.getId());
//            List<AttrEntity> attrEntityList = attrService.list(wrapper);
//            List<AttrResponseVo> attrResponseVoList = attrEntityList.stream().map((attrEntity) -> {
//                AttrResponseVo attrResponseVo = new AttrResponseVo();
//                BeanUtils.copyProperties(attrEntity, attrResponseVo);
//                return attrResponseVo;
//            }).collect(Collectors.toList());
//            attrAndGroupVo.setAttrs(attrResponseVoList);
//            result.add(attrAndGroupVo);
//        }

        //另一种方法
        attrGroupEntityList.forEach((attrGroupEntity) -> {
            AttrAndGroupVo attrAndGroupVo = new AttrAndGroupVo();
            BeanUtils.copyProperties(attrGroupEntity, attrAndGroupVo);
            List<AttrEntity> attrEntityList = attrService.list(new QueryWrapper<AttrEntity>().
                    eq("attr_group_id", attrGroupEntity.getId()));
            attrAndGroupVo.setAttrs(attrEntityList.stream().map((attrEntity) -> {
                AttrResponseVo attrResponseVo = new AttrResponseVo();
                BeanUtils.copyProperties(attrEntity, attrResponseVo);
                return attrResponseVo;
            }).collect(Collectors.toList()));
            result.add(attrAndGroupVo);
        });
        return result;
    }

    //获取spu下所有分组&关联属性   groupName/attrName/attrValue
    @Override
    public List<SpuItemAttrGroup> getAllAttrGroupAndAttrBySpuId(Long spuId) {
        return baseMapper.getAllAttrGroupAndAttrBySpuId(spuId);
    }

}